# !/usr/bin/env python
# -*-coding:utf-8 -*-

"""
# File       : process_name_option_view.py
# Time       ：2023/11/4 15:58
# Author     ：Y-aong
# version    ：python 3.10
# Description：流程名称option
"""
from sqlalchemy import distinct

from apis.options import option_ns
from apis.options.schema.option_schema import OptionSchema
from apis.options.views import option_model
from apis.orderlines.models import Process
from public.api_utils.api_decorator import handle_api_error
from public.base_model import db
from public.base_response import generate_response, MarshalModel
from public.base_view import AbstractView

process_name_opt_marshal = MarshalModel('process_name_opt', option_ns)


class ProcessNameOptionView(AbstractView):
    url = '/process_name_opt'

    def __init__(self, *args, **kwargs):
        super(ProcessNameOptionView, self).__init__(*args, **kwargs)

    @handle_api_error()
    @option_ns.doc(description='流程名称选项')
    @option_ns.marshal_with(process_name_opt_marshal.list_response(option_model, is_db=False))
    def get(self):
        """流程名称选项"""
        objs = db.session.query(
            distinct(Process.process_name).label('label'), Process.process_name.label('value')
        ).filter(Process.active == 1).all()
        options = OptionSchema().dump(objs, many=True)
        return generate_response(options)
